package www.dianbaozu.com.ui.my

import android.view.View
import androidx.activity.OnBackPressedCallback
import androidx.navigation.Navigation
import com.netease.nim.uikit.api.NIMHelper
import www.dianbaozu.com.R
import www.dianbaozu.com.databinding.FragmentModifyPhoneBinding
import www.wen.com.common.lib.base.fragment.BaseVMFragment
import www.wen.com.common.lib.extension.getColorAgency
import www.wen.com.common.lib.extension.observeObject
import www.wen.com.common.lib.extension.resetStartDestination
import www.wen.com.common.lib.helper.loading.KeyHelper
import www.wen.com.common.lib.helper.loading.TitleBarUniversalAdapter
import www.wen.com.common.lib.util.SpUtil

/**
 * @author WEN
 * @Description: 修改手机
 * @date 2020//25 17:48
 */
class ModifyPhoneFragment : BaseVMFragment<FragmentModifyPhoneBinding>(), View.OnClickListener {

    override fun isEnableLoadingHelper(): Boolean = true

    override fun getTitleBarUniversalAdapter(): TitleBarUniversalAdapter =
            TitleBarUniversalAdapter(
                    activity = requireActivity(),
                    background = getColorAgency(R.color.color000000),
                    action = ::titleAction)

    override fun getLayoutId(): Int = R.layout.fragment_modify_phone

    override fun lazyInitView() {
        observeListen()
        addBackPressedCallBack()
    }

    /**
     * 返回键直接关闭该页面
     */
    private fun addBackPressedCallBack() {
        requireActivity().onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) {

            override fun handleOnBackPressed() {
                if(hasFragment(ModifyPhoneStepThirdFragment::class.java)){
                    NIMHelper.logout()
                    SpUtil.clearAll()
                    requireActivity().resetStartDestination(R.navigation.navigation, R.id.loginFragment)
                }else{
                    Navigation.findNavController(mRootView).navigateUp()
                }
            }
        })
    }

    fun hasFragment(clazz: Class<*>):Boolean{
        childFragmentManager.fragments.forEach {
            if(it.javaClass.isAssignableFrom(clazz))  return true
        }
        return false
    }

    private fun observeListen() {
        observeObject(KeyHelper.NEXT_STEP_OR_PREVIOUS_KEY, this, Boolean::class.java) {
            if(this) nextStep() else previousStep()
        }
    }


    override fun onClick(v: View) {

    }

    private fun nextStep(){
        dataBinding.stepView.next()
    }

    private fun previousStep(){
        dataBinding.stepView.previous()
    }

}



